﻿using System;
using System.Data;
using ConsoleApplication2.Dao;
using ConsoleApplication2.Entity;
using ConsoleApplication2.pushApi;
using Quartz;

namespace ConsoleApplication2.job
{
    public class HelloJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Hello:==>" + DateTime.Now);
        }
    }

    public class VideoBooking : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            string sql = @"
                    SELECT
	                    tvlb.VideoLiveId,
	                    tvl.Name AS VideoName,
	                    (SELECT tp.Name FROM topmember..T_User tu 
		                    LEFT JOIN topmember..T_Person tp ON tu.PersonId = tp.Id
		                    WHERE tu.IsDel = 0 AND tu.IsUsed = 1 AND tu.Id = tvlb.UserId
			                    AND tp.IsDel = 0 AND tp.IsUsed = 1)UserName,
	                    tvl.StartTime,
	                    tvl.EndTime,
                        tvlb.Id VideoLiveBookingId,
                        tvlb.UserId	
                    FROM
	                    toponline..T_VideoLiveBooking tvlb
	                    LEFT JOIN toponline..T_VideoLive tvl ON tvl.Id = tvlb.VideoLiveId
                    WHERE
	                    tvlb.IsDel = 0 AND tvlb.[Count] = 0
	                    AND tvl.IsUsed = 1 AND tvl.IsDel = 0
	                    AND tvl.StartTime < DATEADD(mi,30,getdate())
                        AND tvl.StartTime > getdate()";
            DataTable dt = SQLHelper.GetTable(sql);
            foreach (DataRow dr in dt.Rows)
            {
                string tips = string.Format(@"{0}，您预约的{1}课程将在{2} ~ {3}开始直播", dr[2], dr[1], dr[3], dr[4]);
                sql =string.Format(@"
                                INSERT
                                INTO
                                T_Msg(
	                                MsgSubTypeId,
	                                Title,
	                                [Content],
	                                AddUserId,
                                    IsSended
                                )
                                VALUES(
	                                1,
	                                '{0}',
	                                '{0}',
	                                1,
                                    1
                                )
                                SELECT @@IDENTITY", tips);
                var msgId = SQLHelper.GetMsgId(sql);
                MessagePara par = new MessagePara
                {
                    Title = tips,
                    MessageId = msgId,
                    MessageType = 1 ,        //刚插入的信息的id
                    UserIds = dr[6].ToString()
                };
                string json = JsonHelper.SerializeObject(par);
                string url = HttpAddress.VideoBookingAddress;
                HttpClientHelp.sendMessage(url, json);

                //发送完之后更新用户预约表为已发送状态
                sql = string.Format("UPDATE toponline..T_VideoLiveBooking SET [Count] = 1 WHERE Id = '{0}'", dr[5]);
                SQLHelper.ExcuteSQL(sql);
            }


            Console.WriteLine("直播预约:==>" + DateTime.Now);


        }
    }


    public class TestJob
    {
        private int _tempInt;

        public int TempInt
        {
            set
            {
                if (value > 100)
                    _tempInt = value;
                else
                    _tempInt = 0;
            }
            get
            {
                if (_tempInt > 0)
                    return 1;
                return 0;
            }
        }
    }


    public class Hello : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Hello word:==>" + DateTime.Now);
        }
    }

  
}
